#include "config.h"

#include "sftmr.h"
#include "task.h"
#include "list.h"

#include "fifo.h"

#include <string.h>

task_t *task1, *task2, *task3, *task4;
sftmr_t *timer1, *timer2, *timer3, *timer4;

uint32_t notify_wValue = 0x20;
void task1_func(void *arg)
{
    // debug("task1: %s\n", (char*)arg);
    // if(notify_wValue == 0x20){
        debug("send notify to task2\n");
        // notify_wValue = 0x20;
        task_notify(task2, 0x21, ACTION_SET_BITS, NULL);
        // notify_wValue = 0x21;
        task_notify(task3, 0x02, ACTION_SET_BITS, NULL);
    // } else {
    //     debug("send notify to task3\n");
    //     task_notify(task3, notify_wValue, ACTION_SET_BITS, NULL);
    //     notify_wValue = 0x20;
    // }
    task_sleep(NULL, 2000);
}

void task2_func(void *arg)
{
    uint32_t notify_value;

    debug("task2: %s\n", (char*)arg);
    int ret = task_notifyWait(0x02, 0x02, &notify_value, configMAX_DELAY);
    if(ret == retTRUE){
        debug("task2_func: receive notify, value = %#x\n", notify_value);
    }else if(ret == retTIMEOUT){
        debug("task2_func: timeout\n");
    }else if(ret == retBLOCKED){
        debug("task2_func: BLOCKED\n");
    }
}

void sftmr_func(void *arg)
{
    debug("------------sftmr_func: arg = %s------------\n", (char*)arg);
    // if (strcmp((char*)arg, "sftmr3") == 0) {
        // debug("sftmr_func: delete timer1\n");
        // sftmr_delete(timer1);
        // sftmr_delete(timer2);
        // sftmr_delete(timer3);
        // sftmr_delete(timer4);

    // }
    // if (strcmp((char*)arg, "sftmr2") == 0){
        // debug("sftmr_func: delete timer2\n");
        // sftmr_delete(timer2);
    // }
    // sftmr_delete(timer3);
    // task_sched_process();
    debug("----------------------------------------------------\n");
}

void task_func(void *arg)
{
    debug("------------task_func: arg = %s------------\n", (char*)arg);
    // if(task1 != NULL){
    //     task_delete(task1);
    //     task1 = NULL;
    // }
    // if(task2 != NULL){
    //     task_delete(task2);
    //     task2 = NULL;
    // }
    // if(task3 != NULL){
    //     task_delete(task3);
    //     task3 = NULL;
    // }
    debug("----------------------------------------------------\n");
    task_sleep(NULL, 1000);
}

// void task_idleTask(void)
// {
//     // debug("task_idleTask\n");
// }

fifo_t fifo1;
uint8_t fifo1_buf[1024];

int main(int argc, char* argv[]) 
{
    interrupt_init();

    fifo_init(&fifo1, fifo1_buf, sizeof(fifo1_buf));
    uint8_t read_buf[1024] = {0};
        debug("fifo1_used:%d, fifo1_free:%d\n", fifo_get_used(&fifo1), fifo_get_free(&fifo1));
        fifo_write(&fifo1, "hello world", 11);
        debug("fifo1_used:%d, fifo1_free:%d\n", fifo_get_used(&fifo1), fifo_get_free(&fifo1));
        fifo_read(&fifo1, read_buf, 11);
        debug("read_buf:%s\n", read_buf);
        debug("fifo1_used:%d, fifo1_free:%d\n", fifo_get_used(&fifo1), fifo_get_free(&fifo1));

    for (;;){
    }

    timer1 = sftmr_create(NULL, sftmr_func, "sftmr1");
    timer2 = sftmr_create(NULL, sftmr_func, "sftmr2");
    timer3 = sftmr_create(NULL, sftmr_func, "sftmr3");
    timer4 = sftmr_create(NULL, sftmr_func, "sftmr4");

    sftmr_start(timer1, 1000, 1000);
    sftmr_start(timer2, 1000, 1000);
    sftmr_start(timer3, 1000, 1000);
    sftmr_start(timer4, 1000, 1000);

    task1 = task_create(NULL, task1_func, "task1_arg", 1);
    task2 = task_create(NULL, task2_func, "task2_arg", 2);
    task3 = task_create(NULL, task2_func, "task3_arg", 2);

    // task1 = task_create(NULL, task_func, "task1", 3);
    // task2 = task_create(NULL, task_func, "task2", 2);
    // task3 = task_create(NULL, task_func, "task3", 4);
    
    debug("main: start\n");
    debug("task_t size:%ld\n", sizeof(task_t));
    for(;;){
        sftmr_process();
        task_sched_process();
    }

    return 0;
}
